package org.dizem.intellipainter.component.color;

import org.dizem.intellipainter.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * A color-chooser component like the default painter of Windows OS
 * <p/>
 * User: dizem@126.com
 * Time: 12-2-18 下午2:24
 */
public class ColorChooser extends JPanel {

	private static final Font FONT = new Font("微软雅黑", Font.PLAIN, 13);
	private final ColorButton colorButton1 = new ColorButton(new Color(0xFF7F27), "颜色1", true);
	private final ColorButton colorButton2 = new ColorButton(new Color(0x99D9EA), "颜色2", false);
	final ColorPickerBox pickerBox = new ColorPickerBox(new ColorPickCallback() {
		public void onSelectColor(Color color) {
			if (colorButton1.isSelected()) {
				colorButton1.setColor(color);
			} else {
				colorButton2.setColor(color);
			}
		}
	});
	private final PickColorButton pickColorButton;

	public void init(JApplet applet) {
		pickColorButton.init(applet);
	}

	public ColorChooser(final MainFrame mainFrame) {
		pickColorButton = new PickColorButton(mainFrame, new ColorPickCallback() {
			public void onSelectColor(Color color) {
				pickerBox.addColor(color);
				if (colorButton1.isSelected()) {
					colorButton1.setColor(color);
				} else {
					colorButton2.setColor(color);
				}
			}
		});

		setLayout(new BorderLayout());
		add(createCenter(), BorderLayout.CENTER);
		add(createSouth(), BorderLayout.SOUTH);
		setPreferredSize(new Dimension(400, 100));
		setMaximumSize(new Dimension(400, 100));
		setMinimumSize(new Dimension(400, 100));

		colorButton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorButton2.setSelected(!colorButton1.isSelected());
			}
		});
		colorButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				colorButton1.setSelected(!colorButton2.isSelected());
			}
		});
	}

	private JLabel createSouth() {
		JLabel label = new JLabel("颜色", JLabel.CENTER);
		label.setFont(FONT);
		label.setForeground(Color.GRAY);
		label.setPreferredSize(new Dimension(300, 20));
		return label;
	}

	private JPanel createCenter() {
		JPanel center = new JPanel(new FlowLayout());
		center.add(colorButton1);
		center.add(colorButton2);
		center.setPreferredSize(new Dimension(350, 80));
		center.setSize(new Dimension(350, 80));

		center.add(pickerBox);
		center.add(pickColorButton);
		return center;
	}

	public Color getSelectedColor1() {
		return colorButton1.getColor();
	}

	public Color getSelectedColor2() {
		return colorButton2.getColor();
	}


}
